# -*- encoding:utf-8 -*-

from verta.libs.default.views import DefaultView

def decorator_handler(decorator):
    def new_decorator(function):
        handler = decorator(function)
        handler.__name__ = function.__name__
        handler.__doc__ = function.__doc__
        handler.__dict__.update(function.__dict__)
        return handler
    new_decorator.__name__ = decorator.__name__
    new_decorator.__doc__ = decorator.__doc__
    new_decorator.__dict__.update(decorator.__dict__)
    return new_decorator

@decorator_handler
def logged(function):
    '''Decorator where user may be logged to run'''
    def run(*args, **kwargs):
        if not args[0].user.is_anonymous():
            return function(*args, **kwargs)
        else:
            return DefaultView(args[0]).error_message(msg='User must be logged in')
    return run

@decorator_handler    
def anonymous(function):
    def run(*args, **kwargs):
        if args[0].user.is_anonymous():
            return function(*args, **kwargs)
        else:
            return DefaultView(args[0]).error_message(msg='User must not be logged in')
    return run